package cn.admobiletop.adsuyi.adapter.toutiao.loader;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;

import cn.admobiletop.adsuyi.ADSuyiSdk;
import cn.admobiletop.adsuyi.ad.ADSuyiSplashAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.entity.ADSuyiAdSize;
import cn.admobiletop.adsuyi.ad.entity.ADSuyiExtraParams;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiSplashAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.ADSuyiIniter;
import cn.admobiletop.adsuyi.adapter.toutiao.listener.SplashAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.manager.TTAdInitManager;
import cn.admobiletop.adsuyi.adapter.toutiao.util.TTAdUtil;
import cn.admobiletop.adsuyi.config.ADSuyiInitConfig;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 开屏广告加载器
 * @date 2020/3/25
 */
public class SplashAdLoader implements ADSuyiAdapterLoader<ADSuyiSplashAd, ADSuyiSplashAdListener> {
    private SplashAdListener splashAdListener;

    @Override
    public void loadAd(ADSuyiSplashAd splashAd, ADSuyiAdapterParams adapterParams, ADSuyiSplashAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(splashAd)
                && splashAd.getContainer() != null
                && adapterParams != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            splashAd.getContainer().setSplashAdListener(adListener);
            ADSuyiPlatformPosId platformPosId = adapterParams.getPlatformPosId();

            ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
            if (config != null && config.isFilterThirdQuestion()) {
                if (TTAdUtil.withoutLowSystem()) {
                    adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "过滤Splash广告，经过测试头条的广告在安卓5.1及以下版本的手机上可能存在兼容性问题"));
                    return;
                }
            }

            TTAdNative ttAdNative = TTAdInitManager.getInstance().createNative(splashAd.getActivity());
            if (ttAdNative == null) {
                adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "头条SDK createNative失败，可能初始化失败或初始化数据有误"));
                return;
            }

            ADSuyiExtraParams localExtraParams = splashAd.getLocalExtraParams();
            int width = 1080;
            int height = 1920;
            if (localExtraParams != null && localExtraParams.getAdSize() != null) {
                ADSuyiAdSize adSize = localExtraParams.getAdSize();
                if (adSize.getWidth() > 0) {
                    width = adSize.getWidth();
                }

                if (adSize.getHeight() > 0) {
                    height = adSize.getHeight();
                }
            }

            AdSlot adSlot = new AdSlot.Builder()
                    .setCodeId(platformPosId.getPlatformPosId())
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(width, height)
                    .setSplashButtonType(TTAdConstant.SPLASH_BUTTON_TYPE_FULL_SCREEN)
                    .setDownloadType(TTAdUtil.getDisplayDownloadTipsDialogType())
                    .build();

            splashAdListener = new SplashAdListener(splashAd.getContainer()
                    , platformPosId.getPlatformPosId()
                    , adListener);
            ttAdNative.loadSplashAd(adSlot, splashAdListener, (int) splashAd.getPlatformTimeout(adapterParams.getPosId()));
        }
    }

    @Override
    public void onResumed() {

    }

    @Override
    public void onPaused() {

    }

    @Override
    public void release() {
        if (splashAdListener != null) {
            splashAdListener.release();
            splashAdListener = null;
        }
    }
}
